﻿using System;
using JetBrains.Annotations;

namespace DynamicProxyCore
{
	public static class Monad
	{
		public static TResult With<T, TResult>(this T obj, [NotNull]  Func<T, TResult> selector, Func<TResult> @default)
			where T:class
		{
			return obj == null ? @default() : selector(obj);
		}

		public static TResult With<T,TResult>(this T obj,[NotNull] Func<T,TResult> selector, TResult @default = default(TResult))
			where T:class
		{
			return obj.With(selector, () => @default);
		}

		public static void Do<T>(this T obj, [NotNull]  Action<T> action)
			where T : class
		{
			if(obj != null)
			{
				action(obj);
			}
		}


	}
}
